package com.hsq.ndmall.wx.controller;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ObjectUtil;
import com.hsq.ndmall.common.holder.LoginUserHolder;
import com.hsq.ndmall.common.utils.R;
import com.hsq.ndmall.wx.feign.goods.GoodsDetailFeign;
import com.hsq.ndmall.wx.feign.ums.FootPrintFeign;
import com.hsq.ndmall.wx.vo.goods.NewGoodsVo;
import com.hsq.ndmall.wx.vo.ums.FootPrintVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 黄四清
 * @version 1.0.0
 * @ClassName FootprintController.java
 * @Description TODO 浏览足迹
 * @createTime 2021年09月01日 10:18:00
 */
@RestController
@RequestMapping("wx/footprint")
public class FootprintController {


    @Autowired
    LoginUserHolder loginUserHolder;

    @Autowired
    FootPrintFeign footPrintFeign;

    @Autowired
    GoodsDetailFeign goodsDetailFeign;

    @GetMapping("list")
    public R findAll(@RequestParam Integer page, @RequestParam Integer limit) {

        Long id = loginUserHolder.getCurrentUser().getId();  // 拿到用户ID
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("user_id", id);
        paramMap.put("page", page);
        paramMap.put("limit", limit);
        R list = footPrintFeign.list(paramMap);
        List<Map> goodsList = (List<Map>) ((Map) list.get("page")).get("list");
        List<FootPrintVo> newGoodsVoList = new ArrayList<>();
        goodsList.forEach(map -> {
            Integer goodsId = MapUtil.get(map, "goodsId", Integer.class);
            // 根据goodsId查询商品
            R info = goodsDetailFeign.info(goodsId);
            FootPrintVo goods = MapUtil.get(info, "goods", FootPrintVo.class);
            goods.setAddTime(MapUtil.get(map, "addTime", String.class));
            newGoodsVoList.add(goods);
        });
        paramMap.clear();
        paramMap.put("page", page);
        paramMap.put("limit", limit);
        paramMap.put("list", newGoodsVoList);
        paramMap.put("total", ((Map) list.get("page")).get("totalCount"));
        paramMap.put("pages", ((Map) list.get("page")).get("totalPage"));
        return R.ok().put("data", paramMap);
    }

}
